﻿using Base.Api.Models;
using Base.Data.Entity;
using Common.AspNetCore;
using Common.EF;
using System;
using System.Threading.Tasks;

namespace Base.Api.Repository
{
    public class BaseRepository : CoreRepository
    {
        private readonly ICommonModelRepository repository;

        public BaseRepository(IServiceProvider provider, ICommonModelRepository repository)
            : base(provider)
        {
            this.repository = repository;
        }

        public ICommonRepository CommonRepository
        {
            get { return this.Repository; }
        }

        public ICommonModelRepository CommonModelRepository
        {
            get { return this.repository; }
        }

        public async Task SaveAuthRoleGroupModelAsync(SaveAuthRoleGroupModel model)
        {
            await CommitChangesAsync(() =>
            {
                this.Repository.AddEntities(model.AddMemberAuthRoles);
                this.Repository.UpdateEntities(model.UpdateMemberAuthRoles);
                this.Repository.DeleteEntity<t_role_group_auth_role>(model.DeleteMemberAuthRoleIds);
            });
        }

        public async Task SaveAuthRoleModelAsync(SaveAuthRoleModel model)
        {
            await CommitChangesAsync(() =>
            {
                this.Repository.AddEntities(model.AddMemberAuthRoles);
                this.Repository.UpdateEntities(model.UpdateMemberAuthRoles);
                this.Repository.DeleteEntity<t_member_auth_role>(model.DeleteMemberAuthRoleIds);
                this.Repository.AddEntities(model.AddMemberAuthRoleGroups);
                this.Repository.UpdateEntities(model.UpdateMemberAuthRoleGroups);
                this.Repository.DeleteEntity<t_member_auth_role_group>(model.DeleteMemberAuthRoleGroupIds);
            });
        }

        public async Task SaveAuthModelAsync(SaveAuthModel model)
        {
            await CommitChangesAsync(() =>
            {
                this.Repository.AddEntities(model.AddRoleAuthForms);
                this.Repository.UpdateEntities(model.UpdateRoleAuthForms);
                this.Repository.DeleteEntity<t_role_auth_form>(model.DeleteRoleAuthFormIds);
                this.Repository.AddEntities(model.AddRoleAuthFormBtns);
                this.Repository.UpdateEntities(model.UpdateRoleAuthFormBtns);
                this.Repository.DeleteEntity<t_role_auth_form_btn>(model.DeleteRoleAuthFormBtnIds);
                this.Repository.AddEntities(model.AddRoleAuthFormFields);
                this.Repository.UpdateEntities(model.UpdateRoleAuthFormFields);
                this.Repository.DeleteEntity<t_role_auth_form_field>(model.DeleteRoleAuthFormFieldIds);
                this.Repository.AddEntities(model.AddRoleAuthInfos);
                this.Repository.UpdateEntities(model.UpdateRoleAuthInfos);
                this.Repository.DeleteEntity<t_role_auth_info>(model.DeleteRoleAuthInfoIds);
            });
        }

        public async Task CommitChangesByCommonAsync(Action func)
        {
            await CommitChangesAsync(func);
        }
    }
}
